//发现模块tabbar
import 'package:flutter/material.dart';

class CommunitySectionTabMenu extends StatefulWidget {
  final int curIndex;
  final List<String> tabTitles;
  final TabController? tabController;
  final double? fontSize;

  const CommunitySectionTabMenu(
    this.tabTitles, {
    super.key,
    this.curIndex = 0,
    this.tabController,
    this.fontSize = 14,
  });

  @override
  State<StatefulWidget> createState() {
    return _CommunitySectionTabMenuState();
  }
}

class _CommunitySectionTabMenuState extends State<CommunitySectionTabMenu> {
  late int curIndex = widget.curIndex;
  final itemKeys = [];

  @override
  void initState() {
    super.initState();
    widget.tabController?.addListener(_listener);
  }

  void _listener() {
    print('_listener ${widget.tabController?.offset}');
    int tabIndex = widget.tabController?.index ?? 0;
    if (tabIndex != curIndex) {
      curIndex = tabIndex;
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 36,
      child: TabBar(
        tabAlignment: TabAlignment.center,
        controller: widget.tabController,
        indicatorWeight: 0,
        isScrollable: true,
        labelStyle: TextStyle(fontSize: widget.fontSize, fontWeight: FontWeight.w400),
        labelColor: Colors.white.withOpacity(.9),
        unselectedLabelColor: Colors.white.withOpacity(.9),
        labelPadding: EdgeInsets.zero,
        unselectedLabelStyle: TextStyle(fontSize: widget.fontSize, fontWeight: FontWeight.w400),
        indicator: BoxDecoration(
          color: Color(0xffFD2D55),
          borderRadius: BorderRadius.circular(999),
        ),
        tabs: List.generate(
            widget.tabTitles.length,
            (index) => Padding(
                  padding: EdgeInsets.symmetric(horizontal: 12, vertical: 4),
                  child: Text(
                    widget.tabTitles[index],
                  ),
                )),
      ),
    );
  }

  @override
  void dispose() {
    widget.tabController?.removeListener(_listener);
    super.dispose();
  }
}
